﻿#if UNITY_EDITOR

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace AI.Graph.Editor {


    public static class ScriptableObjectExtension {

        public static bool IsSubAssetOf<T> (this T subAsset, ScriptableObject mainAsset) where T : ScriptableObject {
            T[] allSubAssets = mainAsset.GetSubAssetsOfType<T> ();
            foreach (T asset in allSubAssets) {
                if (asset == subAsset) {
                    return true;
                }
            }

            return false;
        }


        public static T[] GetSubAssetsOfType<T> (this ScriptableObject asset) where T : ScriptableObject {
            Object[] allAssets = AssetDatabase.LoadAllAssetsAtPath (AssetDatabase.GetAssetPath (asset));
            List<T> subAssets = new List<T> ();

            foreach (Object obj in allAssets) {
                if (obj == asset) continue;

                if (obj.IsTypeOf<T> ()) {
                    subAssets.Add ((T) obj);
                }
            }

            return subAssets.ToArray ();
        }

    }


}
#endif